<?php

namespace App\Admin\Controllers;

use App\Models\Collection;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class CollectionController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('Virtual Collections');
            $content->description('List');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('Virtual Collections');
            $content->description('Edit');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('Virtual Collections');
            $content->description('Add');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Collection::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->name('Name');
            $grid->abstract('Abstract')->display(function($text) {
                return str_limit($text, 30, '...');
            });
            $grid->cover('Cover')->image();


            $states = [
                'on'  => ['value' => 1, 'text' => '上架', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '下架', 'color' => 'danger'],
            ];

            $grid->is_online('Status')->switch($states);

            $grid->created_at();
            $grid->updated_at();

            $grid->column('Article')->display(function() {
                $artilceCount = $this->getArticleCount();
                if ($artilceCount == 0) {
                    return '还没有文章<br> <a href="' . route('collection_articles.index', ['cid' => $this->id]) . '">添加文章</a>'; 
                } else {
                    return '共' . $artilceCount . '篇文章 <br><a href="' . route('collection_articles.index', ['cid' => $this->id]) . '"> 查看/编辑</a>'; 
                }
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Collection::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name', "Name")->rules('required');
            $form->text('cn_name', "中文名称")->rules('required');
            // $form->text('cn_name', "CN Name")->rules('required');
            // $form->text('cn_name', "CN Name");
            $form->textarea('abstract', "Introduction")->rules('required');
            $form->textarea('cn_abstract', "中文介绍")->rules('required');
            // $form->textarea('cn_abstract', "CN Introduction");
            $form->image('cover', "Cover");
            // $form->image('cn_cover', "CN Cover");
            $states = [
                'on'  => ['value' => 1, 'text' => '上架', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '下架', 'color' => 'danger'],
            ];

            $form->switch('is_online', 'Status')->states($states);
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
            $form->disableViewCheck();
            $form->disableEditingCheck();
            $form->tools(function(\Encore\Admin\Form\Tools $tools) {
                $tools->disableView();
            });
        });
    }
}
